package com.marvels.approve.process.controller;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.marvels.approve.process.dto.DecisionRefusListDTO;
import com.marvels.approve.process.dto.HungUpConditionDTO;
import com.marvels.approve.process.dto.LoanApplyCaseDTO;
import com.marvels.approve.process.dto.RiskParamsDTO;
import com.marvels.approve.process.dto.RiskResultDTO;
import com.marvels.approve.process.service.ILoanApplyCaseLogService;
import com.marvels.approve.process.service.ILoanApplyCaseService;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;

/**工单跟踪
 * @author hejie
 */
@RestController
@RequestMapping("approve/loanapplycase/service/")
public class LoanApplyCaseController {
	@Autowired
	private ILoanApplyCaseService loanApplyCaseService ;
	
	@Autowired
	private ILoanApplyCaseLogService  loanApplyCaseLogService;
	
	/**单子状态
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public MarvelsResultDTO listLoanApplyCase(@RequestBody(required=false) LoanApplyCaseDTO loanApplyCaseDTO){
		try {
			List datas = loanApplyCaseService.findLoanApplyCases(loanApplyCaseDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), datas);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
	/**单子状态
	 * @return
	 */
	@RequestMapping("selflist/{pageNum}")
	@ResponseBody
	public MarvelsResultDTO showSelfLoanApplyCase(@PathVariable Integer pageNum){
		try {
			LoanApplyCaseDTO loanApplyCaseDTO = new LoanApplyCaseDTO();
			loanApplyCaseDTO.setPageNum(pageNum);
			List datas = loanApplyCaseService.findSelfLoanApplyCases(loanApplyCaseDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), datas);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_FAIL...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
	
	/**
	 * 决策拒绝列表
	 * @param pageNum
	 * @return
	 */
	@RequestMapping("decisionRefusList")
	@ResponseBody
	public MarvelsResultDTO decisionRefusList(@RequestBody DecisionRefusListDTO decisionRefusListDTO){
		try {
			List<Map<String, Object>> list = loanApplyCaseService.decisionRefusList(decisionRefusListDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_FAIL...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
	
	/**
	 * 重新取件审批
	 * @param pageNum
	 * @return
	 */
	@RequestMapping("reApproval")
	@ResponseBody
	public MarvelsResultDTO reApproval(@RequestBody DecisionRefusListDTO decisionRefusListDTO){
		try {
			loanApplyCaseService.reApproval(decisionRefusListDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), null);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_FAIL...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
	
	/**
	 * @return
	 */
	@RequestMapping("merge")
	@ResponseBody
	public MarvelsResultDTO saveOrUpdateLoanApplyCase(@RequestBody LoanApplyCaseDTO loanApplyCase){
		try {
			loanApplyCase = this.loanApplyCaseService.saveLoanApplyCase(loanApplyCase);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), loanApplyCase);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_FAIL...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
	
	/**查看取件列表
	 * @return
	 */
	@RequestMapping("getid")
	@ResponseBody
	public MarvelsResultDTO getLoanApplyCase(@RequestBody LoanApplyCaseDTO loanApplyCase){
		try {
			loanApplyCase = this.loanApplyCaseService.findLoanApplyCase(loanApplyCase.getId());
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), loanApplyCase);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_FAIL...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
	
	/**查看取件列表
	 * @return
	 */
	@RequestMapping("transfers")
	public MarvelsResultDTO queryLoanApplyCaseForTransfers(@RequestBody LoanApplyCaseDTO loanApplyCase){
		try {
			List<LoanApplyCaseDTO> loanApplyCases = this.loanApplyCaseService.findLoanApplyCaseForTransfers(loanApplyCase);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), loanApplyCases);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_FAIL...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
	
	/**转件
	 * @param loanApplyCase
	 * @return
	 */
	@RequestMapping("transfersCase")
	public MarvelsResultDTO transfersLoanApplyCase(@RequestBody LoanApplyCaseDTO loanApplyCase) {
		try {
			LoanApplyCaseDTO loanCase  = this.loanApplyCaseService.transfersLoanApplyCase(loanApplyCase);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), loanCase);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_FAIL...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
	
	
	@RequestMapping("/getLoanApplyCase/{loanId}")
	@ResponseBody
	public MarvelsResultDTO getLoanApplyCase(@PathVariable("loanId") String loanId){
		try {
			LoanApplyCaseDTO loanApplyCase =  loanApplyCaseService.findLoanApplyCase(loanId);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), loanApplyCase);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	
	}
	
	
	/**
	 * 查询风险参数数据
	 * @param loanId
	 * @return
	 */
	@RequestMapping("/getRiskParamsInfo")
	@ResponseBody
	public MarvelsResultDTO  getRiskParamsInfo(@RequestBody RiskParamsDTO riskParamsDTO){
		
		try {
			RiskParamsDTO paramsDTO =  loanApplyCaseService.getRiskParamsInfo(riskParamsDTO.getLoanId());
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), paramsDTO);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>loanId"+riskParamsDTO.getLoanId()+",查询风险参数数据失败",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>loanId"+riskParamsDTO.getLoanId()+","+e.getMessage(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	
	
	
	/**
	 * 云保贷计算年缴保费
	 * @param loanId
	 * @return
	 */
	@RequestMapping("/payPremiumAnnualPayment")
	@ResponseBody
	public MarvelsResultDTO  payPremiumAnnualPayment(@RequestBody RiskParamsDTO riskParamsDTO){
		try {
			RiskParamsDTO paramsDTO=  loanApplyCaseService.payPremiumAnnualPayment(riskParamsDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), paramsDTO);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>loanId"+riskParamsDTO.getLoanId()+",计算年缴保费失败",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>loanId"+riskParamsDTO.getLoanId()+",计算年缴保费异常",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	
	
	/**
	 * 更新分险参数信息
	 * @param riskParamsDTO
	 * @return
	 */
	@RequestMapping("/updateRiskInfo")
	@ResponseBody
	public MarvelsResultDTO  updateRiskInfo(@RequestBody  RiskParamsDTO riskParamsDTO){
		try {
			loanApplyCaseService.updateRiskInfo(riskParamsDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), null);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>loanId"+riskParamsDTO.getLoanId()+",更新分险参数信息失败",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>loanId"+riskParamsDTO.getLoanId()+", 更新分险参数信息异常",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	
	
	
	
	
	/**
	 * 试算核定金额
	 * @param riskParamsDTO
	 * @return
	 */
	@RequestMapping("/trialApprovedAmount")
	@ResponseBody
	public MarvelsResultDTO  trialApprovedAmount(@RequestBody RiskParamsDTO riskParamsDTO){
		try {
			RiskResultDTO resultDto =loanApplyCaseService.trialApprovedAmount(riskParamsDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), resultDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>loanId"+riskParamsDTO.getLoanId()+",试算核电金额失败",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>loanId"+riskParamsDTO.getLoanId()+",试算核电金额异常",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	
	
	
	/**
	 * 更换资金源试算
	 * @param riskParamsDTO
	 * @return
	 */
	@RequestMapping("/fundChangeTrial")
	@ResponseBody
	public MarvelsResultDTO  fundChangeTrial(@RequestBody RiskParamsDTO riskParamsDTO){
		try {
			RiskParamsDTO paramsDTO=loanApplyCaseService.fundChangeTrial(riskParamsDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), paramsDTO);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>loanId"+riskParamsDTO.getLoanId()+",更换资金源试算失败",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>loanId"+riskParamsDTO.getLoanId()+",更换资金源试算异常",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	
	
	/**
	 * 挂起操作
	 * @param dto
	 * @return
	 */
	@RequestMapping("/hungUp")
	@ResponseBody
	public MarvelsResultDTO  hungUp(@RequestBody HungUpConditionDTO dto){
		try {
			loanApplyCaseService.hungUp(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), null);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>loanId"+dto.getLoanId()+",挂起操作失败",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>loanId"+dto.getLoanId()+",挂起操作异常",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
}
